#ifndef _registry_h_
#define _registry_h_

/*
 clsquare - closed loop simulation system
 Copyright (c) 2004, Neuroinformatics Group, Prof. Dr. Martin Riedmiller,
 University of Osnabrueck
 
 Author: Sascha Lange
 
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are
 met:
 
   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the University of Osnabrueck nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 DAMAGE. 
 */

#define MAX_ENTRIES 100

class Plant;
class Graphic;
class Controller;

/** Central registry of factory classes that are able to construct instances
    of an unknown class. */
class AbstractRegistry{
public:
  /** this interface declares an abstract factory, that is able to construct
      instances of one class. Each class needs a different implementation of 
      the abstract factory. */
  class Factory {  
  public:
    virtual ~Factory() {}
    virtual void* create() const=0;
  };    

  virtual ~AbstractRegistry();
  
  /** add a factory for the class "name" */
  virtual void add(const char* name, const char* desc, 
		   Factory* factory);
  /** lookup a factory for the requested class "name" */
  virtual Factory* lookup(const char* name) const;

  virtual int listEntries(char* buf, int buf_size) const;

protected:

  AbstractRegistry();              // Abstract class, can not be created
  
  struct Entry {                   // mapping name -> factory
    const char* name;
    const char* desc;
    Factory* factory;
  };

  Entry entries[MAX_ENTRIES];      // list of mappings name -> factory
  int size;                        // number of available factories
};


/** Central registry of factory classes that are able to construct instances
    of plants. Implemented as singleton. */
class PlantFactory : public AbstractRegistry {
public:
  /** Implemented as singleton */
  static PlantFactory* getThePlantFactory();

  Plant* create(const char* name) const;

protected:
  static PlantFactory* the_plantFactory;
};


/** Central registry of factory classes that are able to construct instances
    of Controller. Implemented as singleton. */
class ControllerFactory : public AbstractRegistry {
public:
  /** Implemented as singleton */
  static ControllerFactory* getTheControllerFactory();

  Controller* create(const char* name) const;

protected:
  static ControllerFactory* the_controllerFactory;
};

/** Central registry of factory classes that are able to construct instances
    of Graphic. Implemented as singleton. */
class GraphicFactory : public AbstractRegistry {
public:
  /** Implemented as singleton */
  static GraphicFactory* getTheGraphicFactory();

  Graphic* create(const char* name) const;

protected:
  static GraphicFactory* the_graphicFactory;
};


/** This macro creates an implementation of the abstract factory for the
    class "name" and adds it to the central GRAPHIC factory. */
#define REGISTER_GRAPHIC(name, desc)                                    \
class _graphicfactory_##name : public GraphicFactory::Factory {         \
public:                                                                 \
  _graphicfactory_##name() {                                            \
    GraphicFactory::getTheGraphicFactory()->add(#name, desc, this);     \
  }                                                                     \
  void* create() const { return new name (); }                          \
};                                                                      \
static _graphicfactory_##name *_graphicfactory_instance_##name =        \
  new _graphicfactory_##name ();    

/** This macro creates an implementation of the abstract factory for the
    class "name" and adds it to the central PLANT factory. */
#define REGISTER_PLANT(name, desc)                                      \
class _plantfactory_##name : public PlantFactory::Factory {             \
public:                                                                 \
  _plantfactory_##name() {                                              \
    PlantFactory::getThePlantFactory()->add(#name, desc, this);         \
  }                                                                     \
  void* create() const { return new name (); }                          \
};                                                                      \
static _plantfactory_##name *_plantfactory_instance_##name =            \
  new _plantfactory_##name ();    


/** This macro creates an implementation of the abstract factory for the
    class "name" and adds it to the central CONTROLLER factory. */
#define REGISTER_CONTROLLER(name, desc)                                 \
class _controllerfactory_##name : public ControllerFactory::Factory {   \
public:                                                                 \
  _controllerfactory_##name() {                                         \
    ControllerFactory::getTheControllerFactory()->add(#name, desc, this);\
  }                                                                     \
  void* create() const { return new name (); }                          \
};                                                                      \
static _controllerfactory_##name *_controllerfactory_instance_##name =  \
  new _controllerfactory_##name ();    


#endif
